package lab.logging;

import java.io.Serializable;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.InitialContext;

/**
 *
 * @author Smike
 */
public final class LoggingUtil {
    
    public static final int ARTIST = 0;
    public static final int GENRE = 1;
    public static final int STUDIO = 2;
    public static final int TRACK = 3;
    
    private LoggingUtil() {}
    
    public static void log(int type) {
        try {
            InitialContext ic = new InitialContext();
            QueueConnectionFactory factory = (QueueConnectionFactory) ic.lookup("jms/LogConnectionFactory");
            QueueConnection connection = factory.createQueueConnection();
            
            QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            
            Queue queue = (Queue) ic.lookup("jms/LogMessages");
            QueueSender sender = session.createSender(queue);
            ObjectMessage message = session.createObjectMessage(new LogMessage(type, System.currentTimeMillis()));
            sender.send(message);
            
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public static class LogMessage implements Serializable {
        public int type;
        public long time;

        public LogMessage(int type, long time) {
            this.type = type;
            this.time = time;
        }        
    }
    
}
